package com.itouch.bracelet.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itouch.bracelet.R;
import com.itouch.bracelet.bean.NewFriend;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * cocoaSJ
 */
public class NewFriendsAdapter extends BaseAdapter {
	private ArrayList<NewFriend> list;
	private Context context;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private LayoutInflater infalter;
	private DisplayImageOptions options;

	public NewFriendsAdapter(Context context, ArrayList<NewFriend> list) {
		this.list = list;
		this.context = context;
		infalter = LayoutInflater.from(context);
		options = new DisplayImageOptions.Builder()
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				.showImageOnLoading(R.drawable.default_face)
				.showImageForEmptyUri(R.drawable.default_face)

				.displayer(new RoundedBitmapDisplayer(5))
				.showImageOnFail(R.drawable.default_face).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public NewFriend getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View converView, ViewGroup viewGrop) {
		// TODO Auto-generated method stub
		ViewHolder vh = null;
		if (converView == null) {
			vh = new ViewHolder();
			converView = infalter.inflate(R.layout.adapter_item_nearby,
					viewGrop, false);
			vh.send = (TextView) converView.findViewById(R.id.send);
			vh.dis = (TextView) converView.findViewById(R.id.dis);
			vh.name = (TextView) converView.findViewById(R.id.name);
			vh.head = (ImageView) converView.findViewById(R.id.head);
			converView.setTag(vh);
		} else {
			vh = (ViewHolder) converView.getTag();
		}
		
		NewFriend bean = getItem(position);
		vh.send.setClickable(true);
		vh.send.setText(R.string.agree);
		vh.send.setTextColor(Color.parseColor("#ef6562"));
		vh.send.setBackgroundResource(R.drawable.add_btn);

		vh.send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				listener.add(position);
			}
		});

		vh.dis.setText(bean.getMsg());
		vh.name.setText(bean.getInitiatorName());
//		imageLoader.displayImage(bean.get, vh.head, options);

		return converView;
	}

	private AddLinstener listener;

	public void setListener(AddLinstener listener) {
		this.listener = listener;
	}

	public interface AddLinstener {

		public void add(int position);
	}

	class ViewHolder {
		public ImageView head;
		public TextView name;
		public TextView dis;
		public TextView send;
	}

}
